package edu.mis.modules.accountCaption;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.render.JsonRender;

import edu.mis.core.util.TreeUtil1;
import edu.mis.model.AccountCaption;

public class AccountCaptionController extends Controller {

	public void index() {
		render("accountCaption.html");
	}

	/*
	 * public void list() { Integer page = getParaToInt("page"); Integer rows =
	 * getParaToInt("rows"); Page<AccountCaption> data =
	 * AccountCaption.dao.paginate(page, rows); setAttr("rows", data.getList());
	 * setAttr("total", data.getTotalRow()); renderJson(); }
	 */
	public void list() {
		// 修改数据库字段要对treeutil1进行修改
		render(new JsonRender(TreeUtil1.findTree(Db.find("select * from account_caption ORDER BY captionId ASC")))
				.forIE());//
	}

	public void add() throws Exception {
		Model<AccountCaption> caption = getModel(AccountCaption.class, "");
//		caption.set("isUse", 0);
		try {
		if (caption.save()) {
			setAttr("success", true);
			setAttr("msg", "添加会计科目成功!");
		} else {
			setAttr("success", false);
			setAttr("msg", "添加会计科目失败!");
		}
		}catch(Exception e){
			setAttr("success", false);
			setAttr("msg", "添加会计科目失败!");
		}
		renderJson();
	}

	public void edit() throws Exception {
		Model<AccountCaption> caption = this.getModel(AccountCaption.class, "");
		if (caption.update()) {
			setAttr("success", true);
			setAttr("msg", "修改会计科目成功!");
		} else {
			setAttr("success", false);
			setAttr("msg", "修改会计科目失败!");
		}
		renderJson();
	}

	public void delete() throws Exception {
		String ids = getPara("ids");
		if (AccountCaption.dao.deleteBatch(ids)) {
			setAttr("success", true);
			setAttr("msg", "删除会计科目成功!");
		} else {
			setAttr("success", true);
			setAttr("msg", "删除会计科目失败!");
		}
		renderJson();
	}

	/**
	 * 为下拉菜单赋值<br>
	 */
	public void getMenu() {
		render(new JsonRender(TreeUtil1.findTree(Db.find("select * from account_caption ORDER BY captionId ASC")))
				.forIE());
	}

	/**
	 * 为下拉菜单赋值<br>
	 */
	public void getMenu2() {
		//返回已启用的科目列表
		render(new JsonRender(
				Db.find("select * from account_caption where captionId like '__%' and status=1 ORDER BY captionId ASC")));
	}
}
